package dwz.web.management;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.paxsz.tms.params.PaxcardElement;
import com.paxsz.tms.params.PaxcardTab;
import com.paxsz.tms.params.PaxcardTitle;

import dwz.business.book.Book;
import dwz.business.book.BookServiceMgr;
import dwz.business.book.Chapter;
import dwz.common.util.web.PageInfo;
import dwz.common.util.web.PageUtil;
import dwz.web.BaseController;

@Controller
@RequestMapping(value="/management/book")
public class BookController extends BaseController{
	@Autowired
	private BookServiceMgr bookMgr;
	
	@RequestMapping("")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		List<Book> bookList = bookMgr.searchBook(null, null, 0, 100);
		PageInfo pageInfo = this.getPagingBean(request);
		view.addObject(bookList);
		view.addObject("pagingBean", PageUtil.getPapination(pageInfo, bookMgr.searchBookNum(null)));
		view.setViewName("/management/book/list");
		return view;
	}
	
	@RequestMapping("/add")
	public String add(Model model) {
		return "/management/book/add";
	}
	
	@RequestMapping("/paxcard")
	public String paxcard(Model model) {
		PaxcardTitle title1 = new PaxcardTitle();
		title1.setLabel("info");
		title1.setName("info");
		PaxcardTitle title2 = new PaxcardTitle();
		title2.setLabel("host");
		title2.setName("host");
		PaxcardTitle title3 = new PaxcardTitle();
		title3.setLabel("ctrl");
		title3.setName("ctrl");
		List<PaxcardTitle> titles = new ArrayList<PaxcardTitle>();
		titles.add(title1);
		titles.add(title2);
		titles.add(title3);
		PaxcardElement el1 = new PaxcardElement();
		el1.setType("textbox");
		el1.setLabel("TID");
		el1.setName("TID");
		el1.setEdit("disabled");
		el1.setLength("14");
		PaxcardElement el2 = new PaxcardElement();
		el2.setType("line");
		PaxcardElement el3 = new PaxcardElement();
		el3.setType("textbox");
		el3.setLabel("MID");
		el3.setName("MID");
		el3.setLength("14");
		List<PaxcardElement> elList = new ArrayList<PaxcardElement>();
		elList.add(el1);
		elList.add(el2);
		elList.add(el3);

		PaxcardElement el4 = new PaxcardElement();
		el4.setType("selectbox");
		el4.setLabel("Comm Type");
		el4.setName("CommType");
		el4.setLength("2");
		List<PaxcardElement> elList1 = new ArrayList<PaxcardElement>();
		elList1.add(el4);

		List<Object> tabs = new ArrayList<Object>();
		tabs.add(elList);
		tabs.add(elList1);
		model.addAttribute("titles", titles);
		model.addAttribute("tabs", tabs);
		return "/management/book/paxcard";
	}
	
	@RequestMapping("/edit/{bookId}")
	public String edit(@PathVariable("bookId") int bookId, Model model) {
		Book book = bookMgr.getBook(bookId);
		List<Chapter> chapters = bookMgr.listChapters(bookId);
		
		model.addAttribute(book);
		model.addAttribute("chapters", chapters);
		
		return "/management/book/edit";
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ModelAndView insert(Book book) {
		bookMgr.addBook(book);

		return ajaxDoneSuccess(getMessage("msg.operation.success"));
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(Book book) {
		bookMgr.updBook(book);
		return ajaxDoneError(getMessage("msg.operation.success"));

	}
	
	@RequestMapping("/delete/{bookId}")
	public ModelAndView delete(@PathVariable("bookId") int bookId) {

		bookMgr.delBook(bookId);

		return ajaxDoneError(getMessage("msg.operation.success"));
	}
}
